/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mrstalk;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gabriel
 */
public class ConnectionManager {
    private static Socket chatSocket = null;
    private static ServerSocket chatServer = null;
    private static final int PORT = 49155;

    public static Chat sendCall( String user, String ip )
    {
        try
        {
            chatSocket = new Socket(ip, PORT);
        }
        catch (UnknownHostException e)
        {
            System.err.println("Couldn't connect to " + ip );
        }
        catch (IOException ex) {
            Logger.getLogger(ConnectionManager.class.getName()).log(Level.SEVERE, null, ex);
        }

        return new Chat( chatSocket, user );
    }

    public static void receiveCall()
    {
        try
        {
            chatServer = new ServerSocket( PORT );
            chatSocket = chatServer.accept();
        }
        catch (IOException e)
        {
           //System.out.println(e);
        }
    }

    public static Chat acceptCall( String user )
    {
        return new Chat( chatSocket, user );
    }
}
